var common = require('../../data/data.js')
Page({
    data: {
      commentContent: '',
      comments:[],
    // page: 1,
    // size: 10,
        article: {},
        isAdd :false
    },
    //添加收藏
    addFavorites:function(){
        let article = this.data.article
        wx.setStorageSync(article.id,article)
        this.setData({
            isAdd : true
        })
    },
    
    
    //取消收藏
    cancelFavorites:function(){
        let article = this.data.article
        wx.removeStorageSync(article.id)
        this.setData({
            isAdd :false
        })
    },
    /**生命周期函数--监听页面加载*/
    onLoad:function (options){
        let id = options.id
        //检查当前新闻是否在收藏夹中
        var newarticle = wx.getStorageSync(id)
        //已存在
        if(newarticle != ''){
            this.setData({
                isAdd :true,
                article : newarticle
            })
        }
        //不存在else{
        else{
            let result = common.getNewsDetail(id)//获取新闻内容
            console.log(result.news)
            if(result.code == '200'){
                this.setData({
                    article:result.news,
                    isAdd :false
                })
            }
        }
    },



    onShow() {
      this.getCommentList()
    },
  
    inputContent(e) {
      this.setData({
        commentContent: e.detail.value
      })
    },
  
    async submitComment() {
      const username = 'test'
      const content = this.data.commentContent
      console.log(content)
      if (!content.trim()) {
        wx.showToast({
          title: '评论内容不能为空',
          icon: 'none'
        })
        return
      }
      const that = this;
      wx.request({
        url: 'http://localhost:8080/api/comment',
        method: 'POST',
        data: {
          username,
          content
        },
        success(res) {
          console.log(res.statusCode)
          if (res.statusCode === 200) {
            wx.showToast({
              title: '评论成功',
              icon: 'success'
            })
            that.setData({
              commentContent: ''
            })
            that.getCommentList()
          } else {
            wx.showToast({
              title: '评论失败',
              icon: 'none'
            })
          }
        },
        fail(error) {
          console.error('接口调用失败，错误信息：' + error.message);
        }
      });
    },
  
    async getCommentList() {
      const that = this;
      wx.request({
        url: 'http://localhost:8080/api/comment',
        success(res) {
          const commentList = res.data;
          console.log(commentList);          // 输出从 API 返回的 JSON 数据对象
          if (commentList && commentList.length) {
            that.setData({
              comments: commentList
            })
          } else {
            wx.showToast({
              title: '获取评论失败',
              icon: 'none'
            })
          }
        },
        fail(error) {
          console.error('接口调用失败，错误信息：' + error.message);
        }
      });
    }
})
